<?php
class Model_Template extends MVC_MODEL {
/**
 * @var object SQL
 */
    public $oDb_template;
    private $rsListSiteMap = array();

    public function __construct() {
        parent::__construct();
        $this->oDb_template = new Db_Template();
    }
    public function insertTemplate() {
        return $this->oDb_template->doInsert($this->getAllData(),false);
    }
    public function updateTemplate() {
        return $this->oDb_template->doUpdateTemplate($this->getAllData());
    }

    public function deleteTemplate($id) {
        return $this->oDb_template->doDeleteByPK($id);
    }
    public function check_exit_template_id($id) {
        return $this->oDb_template->check_exit_template_id($id);
    }

    public function updateTemplateFiles() {
        $sTpl_dir = PATH_UPLOAD.'template/temp'.$this->getData('id').'/';
        if(!is_dir($sTpl_dir)) {
            mkdir($sTpl_dir, 0777);
            chmod($sTpl_dir, 0777);
        }
        $this->writeFile( $sTpl_dir.'explanation.html', $this->getData('explanation') );
        $this->writeFile( $sTpl_dir.'damage.html', $this->getData('damage') );
        $this->writeFile( $sTpl_dir.'dagame.html', $this->getData('dagame') );
        $this->writeFile( $sTpl_dir.'rank.html', $this->getData('rank') );
        $this->writeFile( $sTpl_dir.'notes.html', $this->getData('notes') );
        $this->writeFile( $sTpl_dir.'size.html', $this->getData('size') );
    }

    public function readTemplate() {
        if($this->getData('id') > 0) {
            $sTpl_dir = PATH_UPLOAD.'template/temp'.$this->getData('id').'/';
        }else {
            $sTpl_dir = PATH_UPLOAD.'template/default/';
        }

        $this->setData( 'default_explanation', $this->readFile($sTpl_dir.'explanation.html') );
        $this->setData( 'default_damage', $this->readFile($sTpl_dir.'damage.html') );
        $this->setData( 'default_dagame', $this->readFile($sTpl_dir.'dagame.html') );
        $this->setData( 'default_rank', $this->readFile($sTpl_dir.'rank.html') );
        $this->setData( 'default_notes', $this->readFile($sTpl_dir.'notes.html') );
        $this->setData( 'default_size', $this->readFile($sTpl_dir.'size.html') );
    }

    function writeFile($sFile, $sContent){
        $oFt = fopen($sFile, 'w');
        
        $sContent = trim(stripslashes($sContent));
        if($sContent != '')
            fwrite( $oFt, $sContent );
        fclose($oFt);
        return true;
    }

    public function readFile( $sFile ) {
        $oFt = fopen($sFile, 'r');
        $iFileSize = filesize($sFile);
        if($iFileSize > 0)
            $sContent = fread($oFt, $iFileSize);
        else
            $sContent = '';
        fclose($oFt);
        return $sContent;
    }

    public function getListTemplate() {
        return $this->oDb_template->getListTemplate($this);
    }

    public function getInfoTemplate($id) {
        return $this->oDb_template->getRecordsetByPK($id);
    }
    public function dogetAllRecord() {
        return $this->oDb_template->getAllRecord();
    }

    function removedir() {
        $dirname = PATH_UPLOAD.'template/temp'.$this->getData('id');
        if (is_dir($dirname))
            $dir_handle = opendir($dirname);
        if (!$dir_handle)
            return false;
        while($file = readdir($dir_handle)) {
            if ($file != "." && $file != "..") {
                if (!is_dir($dirname."/".$file))
                    unlink($dirname."/".$file);
                else {
                    $a=$dirname.'/'.$file;
                    removedir($a);
                }
            }
        }
        closedir($dir_handle);
        rmdir($dirname);
        return true;
    }
}
